/**
 * Created by TomSean on 2017/4/26.
 */
var MerchantManager = function ($parent) {
    this.$parent = $parent;
    this.sysUtils = new kendoUtils();
    this.init();
    return this;
}

MerchantManager.prototype = {
    init : function () {
        var fieldArray = this.sysUtils.getBindFieldArray(this.$parent.find('.search-panel'));
        var functionArray = [
            {fName : 'find_data', fs : this.findData},
            {fName : 'reset_btn', fs : this.resetInfo}
        ];
        this.models = new bindBaseModel(null, fieldArray, functionArray, null);
        this.sysUtils.bindDoucment(this.models, this.$parent, this);
        Utils.createDataDictDropDownList({div:this.$parent.find('#merchantEnable'), parentModelKey:BASIS.COOP_STATUS.ROOT}, this);
        Utils.createDataDictDropDownList({div:this.$parent.find('#merchantType'), parentModelKey:BASIS.MERCHANT_TYPE.ROOT}, this);
        Utils.createDataDictDropDownList({div:this.$parent.find('#merchantNature'), parentModelKey:BASIS.MERCHANT_NATURE.ROOT}, this);

        this.initTable();
        this.findData();
    },
    /*
     * 重置按钮
     */
    resetInfo:function(e){
        var that = $(e.target).data("that");
        that.sysUtils.setBindFieldNull(that.models.bindFieldArray);
    },
    initTable : function () {
        var _this = this;
        var columns = [
            {
                width : 30,
                headerTemplate : '<input type="checkbox" gridId="merchant_list_data" onclick="gridCheckAll(this)" />',
                template : '<input type="checkbox" name="merchant_id" value="${id}" />'
            },
            {
                title : '商家名称',
                field : 'merchantName'
            },
            {
                title : '商家法人',
                field : 'merchantCorporation'
            },
            {
                title : '工商企业证书',
                field : 'merchantCertificateNo'
            },
            {
                title : '商家所在地址',
                field : 'merchantAddress'
            },
            {
                title : '商家座机',
                field : 'merchantTel'
            },
            {
                title : '联系人',
                field : 'merchantContacts'
            },
            {
                title : '商家类型',
                template : '#:Utils.getModelNameByParentKey(BASIS.MERCHANT_TYPE.ROOT, merchantType)#'
            },
            {
                title : '商家性质',
                template : '#:Utils.getModelNameByParentKey(BASIS.MERCHANT_NATURE.ROOT, merchantNature)#'
            },
            {
                title : '商家信誉等级(级)',
                field : 'merchantReputationLevel'
            },
            {
                title : '合作关系',
                template : '#:Utils.getModelNameByParentKey(BASIS.COOP_STATUS.ROOT, merchantEnable)#'
            },
            {
                title : '经办人',
                field : 'merchantCreateUserName'
            },
            {
                title : '经办时间',
                field : 'merchantCreateTime'
            }
        ];
        this.sysUtils.createGird(this.$parent.find('#merchant_list_data'), {
            selectable : 'single',
            autoSync : true,
            columnMenu : true,
            pageable : {input : true, numeric : false},
            columns : columns,
            change : function(e) {
                var selected = $.map(this.select(), function(itm) {
                    return $(itm).find("input[type=checkbox]").val();
                });
                _this.selectRowId = selected[0];
            }
        });
    },
    findData : function (e) {
        var _this = this;
        if (!isEmpty(e)) {
            _this = $(e.target).data('that');
        }
        var grid = _this.$parent.find('#merchant_list_data').data('kendoGrid');
        var params = _this.sysUtils.getBindDocJson(_this.models.bindFieldArray);
        var dataT = {jsonParam : JSON.stringify(params)};
        var dataSource = _this.sysUtils.getDataSource({url : baseApiPath + 'merchant/list', data : dataT, dataType : 'odata'});
        grid.setDataSource(dataSource);
    },
    pauseData : function (e) {
        var _this = this;
        if (!isEmpty(e)) {
            _this = $(e.target).data('that');
        }
        var ids = '';
        _this.$parent.find('input[type="checkbox"][name="merchant_id"]:checked').each(function (inx, itm) {
            ids += $(itm).val() + ',';
        });
        if (isEmpty(ids)) {
            notification({title : '提示', message : '请勾选需要暂停的合作商家'}, 'info');
            return;
        }
        ids = ids.substring(0, ids.length - 1);
        var dataT = {ids : ids};
        JsonpAjax(baseApiPath + 'merchant/pause', JSON.stringify(dataT), 'json', function (res) {
            _this.findData();
        }, true);
    },
    continueData : function (e) {
        var _this = this;
        if (!isEmpty(e)) {
            _this = $(e.target).data('that');
        }
        var ids = '';
        _this.$parent.find('input[type="checkbox"][name="merchant_id"]:checked').each(function (inx, itm) {
            ids += $(itm).val() + ',';
        });
        if (isEmpty(ids)) {
            notification({title : '提示', message : '请勾选需要重新合作的商家'}, 'info');
            return;
        }
        ids = ids.substring(0, ids.length - 1);
        var dataT = {ids : ids};
        JsonpAjax(baseApiPath + 'merchant/continue', JSON.stringify(dataT), 'json', function (res) {
            _this.findData();
        }, true);
    },
    openNewWindow : function (e) {
        var _this = this;
        if (!isEmpty(e)) {
            _this = $(e.target).data('that');
        }
        _this.isEdit = false;
        _this.entity = undefined;
        _this.openWindow("添加商户信息");
    },
    openEditWindow : function (e) {
        var _this = this;
        if (!isEmpty(e)) {
            _this = $(e.target).data('that');
        }

        var id = _this.selectRowId;
        if (isEmpty(id)) {
            notification({title : '提示', message : '请选中需要编辑的商户'}, 'info');
            return;
        }
        _this.isEdit = true;
        JsonpAjax(baseApiPath + 'merchant/' + id, undefined, 'json', function (res) {
            _this.entity = res;
            _this.openWindow("编辑商户信息");
        }, true);
    },
    openWindow : function ( title ) {
        var _this = this;
        _this.editDiv = _this.sysUtils.openWindow(_this.editDiv, {
            title : title,
            width : '700px',
            height : '580px',
            resizable : false,
            content : "views/parkLotCenter/discountManage/merchant_edit.html"
        }, true);
    }
};

var MerchantEdit = function ($parent) {
    this.$parent = $parent;
    this.sysUtils = new kendoUtils();
    this.init();
    return this;
}

MerchantEdit.prototype = {
    init : function () {
        var _this = this;
        var fieldArray = this.sysUtils.getBindFieldArray(this.$parent);
        var functionArray = [
            {fName : 'save_info', fs : this.saveInfo},
            {fName : 'cancel', fs : function () { $('#' + merchantManager.editDiv).data('kendoWindow').close(); merchantManager.findData(); } }
        ];
        this.models = new bindBaseModel(null, fieldArray, functionArray, null);
        this.sysUtils.bindDoucment(this.models, this.$parent, this);
        Utils.createDataDictDropDownList({div:this.$parent.find('#merchantType'), parentModelKey:BASIS.MERCHANT_TYPE.ROOT, select:Utils.getModelValueByModelKey(BASIS.MERCHANT_TYPE.TYPE_HOTEL)}, this);
        Utils.createDataDictDropDownList({div:this.$parent.find('#merchantNature'), parentModelKey:BASIS.MERCHANT_NATURE.ROOT, select:Utils.getModelValueByModelKey(BASIS.MERCHANT_NATURE.PRIVATE_ENTERPRISE)}, this);
        Utils.createDataDictDropDownList({div:this.$parent.find('#merchantEnable'), parentModelKey:BASIS.COOP_STATUS.ROOT, select:Utils.getModelValueByModelKey(BASIS.COOP_STATUS.COOP_ING)}, this);
        this.$parent.find('[name="indLogin"]').bind('change', function () {
            console.log($(this).val());
            if ($(this).val() == '0') {
                _this.$parent.find('#nickName').attr('disabled', 'disabled');
                _this.$parent.find('#loginAdmin').attr('disabled', 'disabled');
                _this.$parent.find('#loginPassword').attr('disabled', 'disabled');
            } else {
                _this.$parent.find('#nickName').removeAttr('disabled');
                _this.$parent.find('#loginAdmin').removeAttr('disabled');
                _this.$parent.find('#loginPassword').removeAttr('disabled');
            }
        });
        this.loadInfo();

    },
    loadInfo : function () {
        var _this = this;
        if (true == merchantManager.isEdit && !isEmpty(merchantManager.entity)) {
            _this.sysUtils.setBindFieldValue(merchantManager.entity);
            if (!isEmpty(merchantManager.entity.openLogin) && merchantManager.entity.openLogin == 1) {
                _this.$parent.find('#indLogin-yes').trigger('click');
            } else {
                _this.$parent.find('#indLogin-no').trigger('click');
            }
            merchantManager.entity = undefined;
        }

    },
    saveInfo : function (e) {
        var _this = this;
        if (!isEmpty(e)) {
            _this = $(e.target).data('that');
        }
        if (validityForm(_this.$parent)) {
            var params = _this.sysUtils.getBindDocJson(_this.models.bindFieldArray);
            params['openLogin'] = _this.$parent.find('[name="indLogin"]:checked').val();
            debugger;
            var dataT = {'entityParam':params};
            JsonpAjax(baseApiPath + 'merchant/save', JSON.stringify(dataT), 'json', function (res) {
                $('#' + merchantManager.editDiv).data('kendoWindow').close();
                merchantManager.findData();
            }, true);
        }
    },

}